package com.springboot_gradrecruit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.springboot_gradrecruit.common.PageRequest;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.mapper.EnterpriseMapper;
import com.springboot_gradrecruit.mapper.VideoMapper;
import com.springboot_gradrecruit.model.dto.video.AddVideoRequest;
import com.springboot_gradrecruit.model.dto.video.PageVideoRequest;
import com.springboot_gradrecruit.model.dto.video.UpdateVideoRequest;
import com.springboot_gradrecruit.model.entity.Enterprise;
import com.springboot_gradrecruit.model.entity.Job;
import com.springboot_gradrecruit.model.entity.Video;
import com.springboot_gradrecruit.model.vo.video.VideoVO;
import com.springboot_gradrecruit.service.VideoService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author zr
* @description 针对表【video】的数据库操作Service实现
* @createDate 2024-11-14 13:51:36
*/
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video>
    implements VideoService {

    @Resource
    private VideoMapper videoMapper;

    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Override
    public Long addVideo(AddVideoRequest addVideoRequest, HttpServletRequest request) {

        //1. 获取参数
        Video video = new Video();
        //2. 设置参数
        video.setVideoStartTime(addVideoRequest.getVideoStartTime());
        video.setVideoEndTime(addVideoRequest.getVideoEndTime());
        video.setVideoRemarks(addVideoRequest.getVideoRemarks());
        video.setVideoStatus(addVideoRequest.getVideoStatus());
        video.setVideoCertificationStatus(addVideoRequest.getVideoCertificationStatus());
        video.setVideoCertificationRefuse(addVideoRequest.getVideoCertificationRefuse());
        video.setVideoUserId(Long.valueOf(addVideoRequest.getVideoUserId()));
        video.setVideoEnterpriseId(Long.valueOf(addVideoRequest.getVideoEnterpriseId()));
        //1.1查询出相关的公司
        Enterprise enterprise = enterpriseMapper.selectOne(new QueryWrapper<Enterprise>().eq("enterprise_id", addVideoRequest.getVideoEnterpriseId()));
        video.setVideoCompanyLogo(enterprise.getEnterpriseLogoUrl());
        video.setVideoCompanyName(enterprise.getEnterpriseName());
        video.setVideoName(addVideoRequest.getVideoName());
        video.setVideoUrl(addVideoRequest.getVideoUrl());

        //2. 保存宣讲会信息
        int insert = videoMapper.insert(video);

        //3.返回
        return (long) insert;
    }

    @Override
    public VideoVO queryVideoByEnterpriseUserId(Long enterpriseUserId) {
        //1.构建查询条件
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("video_enterprise_id",enterpriseUserId);
        //2.执行查询
        Video video =videoMapper.selectOne(queryWrapper);
        //3.返回
        return BeanUtil.copyProperties(video,VideoVO.class);
    }

    @Override
    public PageResponse<VideoVO> pageVideo(PageVideoRequest pageVideoRequest) {
        //1.获取参数
        String searchName=pageVideoRequest.getSearchName();
        String videoStatus = pageVideoRequest.getVideoStatus();
        String videoCertificationStatus = pageVideoRequest.getVideoCertificationStatus();

        //2.构建查询条件
        Page<Video> page = pageVideoRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Video> videoPage = lambdaQuery()
                .eq(StrUtil.isNotBlank(videoStatus), Video::getVideoStatus, videoStatus)
                .eq(StrUtil.isNotBlank(videoCertificationStatus), Video::getVideoCertificationStatus, videoCertificationStatus)
                .and(StrUtil.isNotBlank(searchName),
                        w -> w.like( Video::getVideoName, searchName)
                                .or()
                                .like(Video::getVideoCompanyName, searchName))
                .page(page);
        return PageResponse.of(videoPage, VideoVO.class);

    }

    @Override
    public boolean updateById(UpdateVideoRequest updateVideoRequest) {
         //1.获取参数
        Long videoId = updateVideoRequest.getVideoId();
        Date videoStartTime = updateVideoRequest.getVideoStartTime();
        Date videoEndTime = updateVideoRequest.getVideoEndTime();
        String videoRemarks = updateVideoRequest.getVideoRemarks();
        String videoName = updateVideoRequest.getVideoName();
        String videoUrl = updateVideoRequest.getVideoUrl();
        String videoStatus = updateVideoRequest.getVideoStatus();
        String videoCertificationStatus = updateVideoRequest.getVideoCertificationStatus();
        String videoCertificationRefuse = updateVideoRequest.getVideoCertificationRefuse();

        //2.构建更新条件
        LambdaUpdateWrapper<Video> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Video::getVideoId,videoId)
                .set(videoStartTime!=null,Video::getVideoStartTime,videoStartTime)
                .set(videoEndTime!=null,Video::getVideoEndTime,videoEndTime)
                .set(StrUtil.isNotBlank(videoRemarks),Video::getVideoRemarks,videoRemarks)
                .set(StrUtil.isNotBlank(videoName),Video::getVideoName,videoName)
                .set(StrUtil.isNotBlank(videoUrl),Video::getVideoUrl,videoUrl)
                .set(StrUtil.isNotBlank(videoStatus),Video::getVideoStatus,videoStatus)
               .set(StrUtil.isNotBlank(videoCertificationStatus),Video::getVideoCertificationStatus,videoCertificationStatus)
                .set(StrUtil.isNotBlank(videoCertificationRefuse),Video::getVideoCertificationRefuse,videoCertificationRefuse);

        return videoMapper.update(null, updateWrapper)>0;

    }


}




